package de.rts.components;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RailwayMap
{
  private List<RailComponent>                                 paintList           = null;
  private Map<Integer, LinkedList<RailComponent>>             routeMap            = null;
  private Map<String, RailComponent>                          railComponentsIdMap = null;
  private Set<Train>                                          trains              = null;
  private Map<Integer, LinkedHashMap<Integer, List<Segment>>> switchSegmentMap;

  public RailwayMap()
  {
    paintList = new LinkedList<RailComponent>();
    routeMap = new HashMap<Integer, LinkedList<RailComponent>>();
    railComponentsIdMap = new HashMap<String, RailComponent>();
    trains = new LinkedHashSet<Train>();
  }

  public RailwayMap(LinkedList<RailComponent> railComponents)
  {
    this();
    this.paintList = railComponents;
  }

  public void addToPaintList(RailComponent components)
  {
    this.paintList.add(components);
  }

  public List<RailComponent> getPaintList()
  {
    return this.paintList;
  }

  public RailComponent getRailComponentsByIndex(int index)
  {
    return this.paintList.get(index);
  }

  public Map<Integer, LinkedList<RailComponent>> getRouteMap()
  {
    return routeMap;
  }

  public void setRouteMap(Map<Integer, LinkedList<RailComponent>> routeMap)
  {
    this.routeMap = routeMap;
  }

  public void addToRouteMap(int key, LinkedList<RailComponent> value)
  {
    if (!this.routeMap.containsKey(key))
      this.routeMap.put(key, value);
    else
      System.err
        .println("key already exists.value not added.use the overriden method 'addToRouteMap(int key, RailComponent value)' instead");
  }

  public void addToRouteMap(int key, RailComponent value)
  {
    LinkedList<RailComponent> map = this.routeMap.get(key);
    if (map != null)
      map.add(value);
    else
    {
      map = new LinkedList<RailComponent>();
      map.add(value);
      this.routeMap.put(key, map);
    }
  }

  public LinkedList<RailComponent> getRoute(int key)
  {
    return this.routeMap.get(key);
  }

  public Map<String, RailComponent> getRailComponentsIdMap()
  {
    return railComponentsIdMap;
  }

  public void addToIdMap(String key, RailComponent value)
  {
    this.railComponentsIdMap.put(key, value);
  }

  public RailComponent getRailComponentById(String id)
  {
    return this.railComponentsIdMap.get(id);
  }

  public void addTrain(Train train)
  {
    this.trains.add(train);
  }

  public Train getTrainByIndex(int index)
  {
    Train train = null;
    if (index > this.trains.size() || index > 0)
    {
      System.err.println("index '" + index + "' is out of range.");
      return null;
    }
    for (int i = 0; i < index; i++)
    {
      train = this.trains.iterator().next();
    }

    return train;
  }

  public Set<Train> getTrains()
  {
    return trains;
  }

  public Map<Integer, LinkedHashMap<Integer, List<Segment>>> getSwitchSegmentMap()
  {
    return switchSegmentMap;
  }

  public void setSwitchSegmentMap(Map<Integer, LinkedHashMap<Integer, List<Segment>>> switchSegmentMap)
  {
    this.switchSegmentMap = switchSegmentMap;
  }

  public LinkedHashMap<Integer, List<Segment>> getSwitchSegmentUnitByKey(int key)
  {
    return this.switchSegmentMap.get(key);
  }

  public List<Segment> getSwitchSegmentsByKey(int trainId, int unit)
  {
    LinkedHashMap<Integer, List<Segment>> map = this.switchSegmentMap.get(trainId);
    return map.get(unit);
  }
}
